package com.fy.service.imbusiness.controller;

import com.alibaba.fastjson.JSONObject;
import com.fy.common.dto.MessageCollectDto;
import com.fy.common.entity.SkImCollect;
import com.fy.common.util.TokenUtil;
import com.fy.framework.common.model.R;
import com.fy.framework.component.config.LocaleMessage;
import com.fy.service.service.SkImCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhshuo
 * @date 2021/12/27 15:43
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api(tags = "消息收藏")
@RequestMapping("/collect")
public class SkImCollectController {

    LocaleMessage localeMessage;

    SkImCollectService skImCollectService;

    /**
     * 消息收藏
     * @return
     */
    @PostMapping
    @ApiOperation("消息收藏")
    public R<?> collect(@RequestBody MessageCollectDto collectDto){
        try {
            skImCollectService.insert(new SkImCollect().setSkImUserId(TokenUtil.getUserId())
                    .setMsgId(collectDto.getMsgId()).setMsgSource(collectDto.getMsgSource())
                    .setMsgSendUser(collectDto.getMsgSendUser()).setSourceType(collectDto.getSourceType())
                    .setMsgType(collectDto.getMsgType()));
            return R.success(localeMessage.getMessage("im.common.successful"));
        } catch (Exception e) {
            log.error("error",e);
            return R.failedAlter(localeMessage.getMessage("im.common.failed"));
        }
    }

    /**
     * 消息收藏删除
     * @return
     */
    @PostMapping("del")
    @ApiOperation("消息收藏删除")
    public R<?> del(@RequestBody @ApiParam(name="id",value="消息收藏数据ID") JSONObject param ){
        try {
            skImCollectService.deleteById(param.getString("id"));
            return R.success(localeMessage.getMessage("im.common.successful"));
        } catch (Exception e) {
            log.error("error",e);
            return R.failedAlter(localeMessage.getMessage("im.common.failed"));
        }
    }

    /**
     * 消息收藏查看
     * @return
     */
    @PostMapping("list")
    @ApiOperation("消息收藏查看")
    public R<?> list(){
        try {
            return R.success(skImCollectService.collectList());
        } catch (Exception e) {
            log.error("error",e);
            return R.failedAlter(localeMessage.getMessage("im.common.failed"));
        }
    }

}
